package cn.e4j.modular.api.service;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.mapper.Gwat500Mapper;
import cn.e4j.modular.crm.entity.GwcuT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class Gwat500Service{


    @Autowired
    private Gwat500Mapper gwat500Mapper;

    /**
     * 根据微信号 获取司机信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwaaData(Dto inDto) {

        return gwat500Mapper.getGwaa(inDto);
    }

    /**
     * 根据输入信息 获取司机信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwaaData2(Dto inDto) {
        return gwat500Mapper.getGwaa2(inDto);
    }

    /**
     * 获取正式订单明细
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwcsPage(Dto inDto) {
        return gwat500Mapper.getGwcsPage(inDto);
    }


    /**
     * 保存订单运输信息
     *
     * @param
     */
    @Transactional
    public Dto insert(SysUser user,Dto inDto) {
        Dto outDto = Dtos.newDto();
        GwcuT gwcu_tPO = JSON.parseObject(JSON.toJSONString(inDto),GwcuT.class);

        Date date = new Date();
        List<GwcuT> gwcu_tPOS = new ArrayList<>();
        JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
        JSONArray orders = inJson.getJSONArray("CHARACTERISTICS");
        gwcu_tPO.setGWCUENT(user.getDBC());
        gwcu_tPO.setGWCUDOCNO("YSDH" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
        gwcu_tPO.setGWCU008(inJson.getString("GWCU008"));
        gwcu_tPO.setGWCU009(inJson.getString("GWCU009"));
        gwcu_tPO.setGWCU010(inJson.getString("GWCU010"));
        gwcu_tPO.setGWCU017(inJson.getString("GWCU017"));
        for (int i = 0, j = orders.size(); i < j; i++) {
            JSONObject item = orders.getJSONObject(i);
            gwcu_tPO.setGWCUSEQ(BigDecimal.valueOf(i + 1));
            gwcu_tPO.setGWCU001(item.getString("GWCU001"));
            gwcu_tPO.setGWCU002(item.getString("GWCU002"));
            gwcu_tPO.setGWCU003(item.getString("GWCU003"));
            gwcu_tPO.setGWCU004(item.getBigDecimal("GWCU004"));
            gwcu_tPO.setGWCU012(item.getString("GWCU012"));
            gwcu_tPO.setGWCU013(item.getString("GWCU013"));
            gwcu_tPO.setGWCU014(item.getString("GWCU014"));
            gwcu_tPO.setGWCU015(item.getString("GWCU015"));
            gwcu_tPO.setGWCU030(item.getString("GWCU030"));
            gwcu_tPO.setGWCU031(item.getBigDecimal("GWCU031"));
            //gwcu_tPO.setGWCUDOCDATE(date);
            gwcu_tPO.setGWCUDOCDATE(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            gwcu_tPO.setGWCUUSER(user.getID());
            gwcu_tPO.setGWCUGRUP(user.getORG_ID());
            gwcu_tPO.setGWCUDATE(LocalDate.now());
           
            gwcu_tPO.setGWCUTIME(DateUtils.formatDate(date, "HH:mm:ss"));
            gwcu_tPO.setGWCUORIG(user.getORG_ID());
            gwcu_tPO.setGWCUORIU(user.getID());
            gwcu_tPO.setGWCUACTI("N");
            gwcu_tPO.insert();
            gwcu_tPOS.add(gwcu_tPO);

        }
        outDto.setAppCode(200);
        return outDto;
    }

    /**
     * 获取客户所在区域的业务人员
     *
     * @param inDto
     * @return
     */
//    public List<Dto> getGwaa(Dto inDto) {
//        inDto.put("GWAAENT", "80");
//        return gwat500Mapper.getGwaaData(inDto);
//    }

}
